#include "led.h"

Led::Led(uint8_t p, uint8_t num)
    : pin(p), state(false), red(255), green(255), blue(255),
      strip(num, p, NEO_GRB + NEO_KHZ800) {}

void Led::begin() {
    strip.begin();
    strip.show(); // 清空灯
}

void Led::on() {
    state = true;
    strip.setPixelColor(0, strip.Color(red, green, blue));
    strip.show();
}

void Led::off() {
    state = false;
    strip.clear();
    strip.show();
}

void Led::toggle() {
    if (state)
        off();
    else
        on();
}

void Led::randomColor() {
    // 随机生成 RGB 三个通道的值（0~255）
    uint8_t r = random(0, 256);
    uint8_t g = random(0, 256);
    uint8_t b = random(0, 256);

    strip.setPixelColor(0, strip.Color(r, g, b)); // 设置像素颜色
    strip.show(); // 更新显示

}

void Led::setColor(uint8_t r, uint8_t g, uint8_t b) {
    red = r;
    green = g;
    blue = b;
    if (state) { // 如果当前亮着，立即更新颜色
        strip.setPixelColor(0, strip.Color(red, green, blue));
        strip.show();
    }
}
